<?php

namespace App\Http\Controllers\Admin;

use App\Http\Resources\LeagueCollection;
use App\Models\League;
//use App\Validates\RoleValidate;
use Illuminate\Http\Request;

class LeaguesController extends AdminController
{
    public function __construct()
    {
        parent::__construct();
        $this->middleware('auth:api');
    }

    public function LeagueList(Request $request, League $league)
    {
        $search_data = json_decode($request->get('search_data'), true);
        if ($search_data['name']) {
            $league = $league->columnLike('name', $search_data['name']);
        }
        return new LeagueCollection($league->get());
    }

    public function addEditLeague(Request $request, League $league/*, RoleValidate $validate*/)
    {
        $update_data = $request->only('id', 'name', 'level', 'description');
        $league_id = $request->post('id', 0);
        if ($league_id > 0) {
            $league = $league->findOrFail($league_id);
            //$rest_validate = $validate->updateValidate($update_data, $league_id);
        } else {
            //$rest_validate = $validate->storeValidate($update_data);
        }

        //if ($rest_validate['status'] === false) return $this->failed($rest_validate['message']);

        //$res = $league->saveData($update_data);
        $res = $league->fill($update_data)->save();

        if ($res) return $this->message('操作成功');
        return $this->failed('内部错误');
    }

    public function destroy(League $league/*, RoleValidate $roleValidate*/)
    {
        if (!$league) return $this->failed('找不到角色', 404);
        /*$rest_destroy_validate = $roleValidate->destroyValidate($role);
        if ($rest_destroy_validate['status'] === true) {*/
            $rest_destroy = $league->delete();
            if ($rest_destroy['status'] === true) return $this->message($rest_destroy['message']);
            return $this->failed($rest_destroy['message'], 500);
        /*} else {
            return $this->failed($rest_destroy_validate['message']);
        }*/
    }
}
